package animator.phantom.renderer.imagesource;

/*
    Copyright Janne Liljeblad 

    This file is part of Phantom2D.

    Phantom2D is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Phantom2D is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Phantom2D.  If not, see <http://www.gnu.org/licenses/>.
*/

import animator.phantom.blender.Blender;
import animator.phantom.renderer.*;
import animator.phantom.renderer.param.*;
import animator.phantom.paramedit.panel.ParamEditPanel;
import animator.phantom.renderer.imagesource.*;
import animator.phantom.paramedit.imagesource.SVGImageSourceEditPanel;
import animator.phantom.plugin.PluginUtils;
import animator.phantom.gui.view.*;
import animator.phantom.gui.view.editlayer.*;
import animator.phantom.paramedit.FlipSelect;
import animator.phantom.controller.*;
import giotto2D.filters.transform.FlipFilter;
import giotto2D.filters.merge.AlphaReplace;
import java.awt.*;
import java.awt.image.*;
import java.util.*;

public class SVGImageSource extends MovingBlendedIOP
{
	private BufferedImage img = null;
	public BooleanParam continuousRender = new BooleanParam( false );

	public SVGImageSource()
	{
		this( null );
	}
	
	public SVGImageSource( SVGSource svgSource )
	{
		if( svgSource != null )
		{
			registerFileSource( svgSource );
			name = svgSource.getName();
		}
		else
		{
			name = "SVGSource";
		}

 		registerCoords( new AnimatedImageCoordinates( this ) );

		registerMovingBlendParams();
		registerParameter( continuousRender );

		setAsSource();
		setIOPToHaveSwitches();
	}
	
	public ParamEditPanel getEditPanelInstance()
	{
		return new SVGImageSourceEditPanel( this );
	}

	public void doImageRendering( int frame, Vector sourceImages )
	{
		//--- Continuous render: SVG as fullscreen image and transform svg document, then do top-left aligned blend.
		if( continuousRender.get() )
		{
			continuosRender(frame);
			return;
		}

		//--- Ok, it's non-continuous render.
		BufferedImage img = ( (SVGSource) getFileSource() ).getImageFromDocument();

		renderMovingBlendedImage( frame, img );
	}

	private void continuosRender( int frame )
	{
		//--- Set motion blur, blend mode and interpolation.
		boolean motionBlur = getCurrentMotionBlur();
		int blendMode = getCurrentBlendMode(); 
		int interpolation = getCurrentInterpolation();

		//--- if leaf, create rendered image
		if( renderedImage == null )
			renderedImage = PluginUtils.createScreenCanvas();

		if( !motionBlur )
		{
			BufferedImage img = ( (SVGSource) getFileSource() ).
				getTranslatedImageFromDocument( 
								(float) frame,
								getCoords(),
								ProjectController.getScreenSize(),
								flipTrans.get() == FlipSelect.HORIZONTAL,
								flipTrans.get() == FlipSelect.VERTICAL  );

			float opacityVal = opacity.getValue( frame ) / 100.0f;// normalize

			Blender.doAlignedBlend( renderedImage,
						img,
						opacityVal,
						blendMode,
						null,
						useOverRule.get() );
		}
		else 
		{
			Blender.doSVGMotionBlurBlend(  	frame,
							renderedImage,
							(SVGSource) getFileSource(),
							this,
							blendMode,
							interpolation,
							flipTrans.get() == FlipSelect.HORIZONTAL,
							flipTrans.get() == FlipSelect.VERTICAL,
							ProjectController.getScreenSize(),
							null,
							useOverRule.get() );
		}
	}

	public Rectangle getImageSize() 
	{
		int w = getFileSource().getImageWidth();
		int h = getFileSource().getImageHeight();
		//--- ?
		if( w == -1 || h == -1 )
		{
			System.out.println("SVGImageSource.getImageSize() asked when image size not known. Loading doc to find out." );
		 	((SVGSource) getFileSource()).loadData();
			w = getFileSource().getImageWidth();
			h = getFileSource().getImageHeight();
		}
		return new Rectangle( w, h );
	}

	public ViewEditorLayer getEditorlayer()
	{
		return new SVGSourceEditLayer( this );
	}

}//end class
